<?php

namespace app\api2\controller;

use \app\common\controller\Api;


use \think\Db;


class Log extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $needWorkerLogin = ['getEvaluateList', 'getMoneyLogList', 'getClearingLogList'];

    /**
     * 获取评价记录列表
     * @throws \think\exception\DbException
     */
    public function getEvaluateList()
    {
        if ($this->request->isPost()) {
            $page = trim($this->request->post('page'));

            $data = (array)$this->_data;
            if (!$page) {
                $this->success('缺少必要参数', [], 201);
            }
            $evaluateList = Db::table('fa_evaluate')
                ->alias('a')
                ->field('a.* ,a.id ,a.worker_id , b.main_image, b.unit_price, b.area')
                ->join('fa_order b', 'a.order_id = b.id')
                ->where('a.worker_id', $data['user_id'])
                ->order('createtime desc')
                ->paginate(5, false, ['page' => $page]);

            $this->success('ok', ['evaluateList' => $evaluateList->items(), 'total' => $evaluateList->total()], 200);
        }
    }

    /**
     * 获取资金收入记录列表
     * @throws \think\exception\DbException
     */
    public function getMoneyLogList()
    {
        if ($this->request->isPost()) {

            $page = trim($this->request->post('page'));
            $status = trim($this->request->post('status'));

            $data = (array)$this->_data;
            if (!$page) {
                $this->success('缺少必要参数', [], 201);
            }
            $moneyLogList = [];
            if ($status == 0) {//本月
                $moneyLogList = Db::table('fa_worker_money_log')
                    ->where('worker_id', $data['user_id'])
                    ->where('type', 1)
                    ->whereTime('createtime', 'month')
                    ->order('createtime desc')
                    ->paginate(10, false, ['page' => $page]);
            } elseif ($status == 1) {//今日
                $moneyLogList = Db::table('fa_worker_money_log')
                    ->where('worker_id', $data['user_id'])
                    ->where('type', 1)
                    ->whereTime('createtime', 'today')
                    ->order('createtime desc')
                    ->paginate(10, false, ['page' => $page]);
            } elseif ($status == 2) {//昨日
                $moneyLogList = Db::table('fa_worker_money_log')
                    ->where('worker_id', $data['user_id'])
                    ->where('type', 1)
                    ->whereTime('createtime', 'yesterday')
                    ->order('createtime desc')
                    ->paginate(10, false, ['page' => $page]);
            } else {
                $this->success('status错误', [], 201);
            }


            $this->success('ok', ['moneyLogList' => $moneyLogList->items(), 'total' => $moneyLogList->total()], 200);
        }
    }

    /**
     * 获取结算记录列表
     * @throws \think\exception\DbException
     */
    public function getClearingLogList()
    {
        if ($this->request->isPost()) {
            $page = trim($this->request->post('page'));

            $data = (array)$this->_data;
            if (!$page) {
                $this->success('缺少必要参数', [], 201);
            }
            $clearingLogList = Db::table('fa_clearing_log')
                ->where('worker_id', $data['user_id'])
                ->order('createtime desc')
                ->paginate(10, false, ['page' => $page]);

            $this->success('ok', ['clearingLogList' => $clearingLogList->items(), 'total' => $clearingLogList->total()], 200);
        }
    }
}